
/*	
 *  -- ************************************************************
	-- Author		:	PARADOX
	-- Create date	:	18-07-2011
	-- Update date	:	PARADOX
	-- Update By	:   14-09-2011
	-- Description	:	Get Date [Version 1.0]
	-- ************************************************************
 */

package com.app.megaprosoft.general;


import android.annotation.SuppressLint;
import java.util.Date;
import java.util.Locale;
import java.text.*;

@SuppressLint("SimpleDateFormat")
public class DateTime {
	
	public static String getTimeNow() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		return df.format(now);
	}
	public static String getTimeNownosemi() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("HHmmss");
		return df.format(now);
	}
	
	public static String getDateNowymd() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
		String dmy = df.format(now);
		int year = Integer.valueOf(dmy.substring(6, 10)).intValue();
		//System.out.println(year);
		if (year > 2550)
			year -= 543;
		//System.out.println( year+ dmy.substring(3, 5) + dmy.substring(0, 2));
		return (year + dmy.substring(3, 5) + dmy.substring(0, 2));
	}

	public static int convertTime(String strTime) {
		if (strTime.length() == 5) { // Format HH:mm
			int IntHH = Integer.valueOf(strTime.substring(0, 2)).intValue();
			int Intmm = Integer.valueOf(strTime.substring(3, 5)).intValue();
			return (IntHH * 60) + (Intmm) ;
		} else {
			if (strTime.length() == 4) { // Format H:mm
				strTime = "0"+strTime;
				int IntHH = Integer.valueOf(strTime.substring(0, 2)).intValue();
				int Intmm = Integer.valueOf(strTime.substring(3, 5)).intValue();
				return (IntHH * 60) + (Intmm) ;
			}else{
				return 9999;
			}
			
		}
	}

	
	public int convertTimess(String strTime) {
		if (strTime.length() == 8) { // Format HH:mm:ss
			int IntHH = Integer.valueOf(strTime.substring(0, 2)).intValue();
			int Intmm = Integer.valueOf(strTime.substring(3, 5)).intValue();
			int Intss = Integer.valueOf(strTime.substring(6, 8)).intValue();
			return (IntHH * 3600) + (Intmm*60)+ (Intss);
		} else {
			return 9999;
		}
			
	}
	
	
	
	public static String getDateNow() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
		String dmy = df.format(now);
		int year = Integer.valueOf(dmy.substring(6, 10)).intValue();
		//System.out.println(year);
		if (year > 2550)
			year -= 543;
		//System.out.println(year);
		return (dmy.substring(0, 2) + "-" + dmy.substring(3, 5) + "-" + year);
	}
	
	public static String getDateNow(String StrFormat) {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat(StrFormat, Locale.ENGLISH);
		String dmy = df.format(now);
		int year = Integer.valueOf(dmy.substring(6, 10)).intValue();
		//System.out.println(year);
		if (year > 2550)
			year -= 543;
		//System.out.println(year);
		return (dmy.substring(0, 2) + "-" + dmy.substring(3, 5) + "-" + year);
	}
	
	public static String getDate(String StrFormat) {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat(StrFormat, Locale.ENGLISH);
		return df.format(now);
	}
	
	public static String getYearNow() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy", Locale.ENGLISH);
		return df.format(now);
	}
	
	public static String getTimeNowss() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");	
		return df.format(now);
	}
	
	public static String getTimeMilliSecondNow() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss:SSS a,z");	
		return df.format(now);
	}
	
	//not sure
	public static String getDateTimeNow() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);
		String dmy = df.format(now);
		int year = Integer.valueOf(dmy.substring(6, 10)).intValue();
		
		if (year > 2550)
			year -= 543;
		
		return (dmy.substring(0, 2) + "-" + dmy.substring(3, 5) + "-" + year + dmy.substring(10, 19));
	}
	
	public static String getDate_Format(String StrFormat) {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat(StrFormat, Locale.ENGLISH);
		return df.format(now);
	}
	
	public static String convertDate(String StrDate) { //dd-MM-YYYY To MM-dd-YYYY
		int IntYear = Integer.valueOf(StrDate.substring(6,10));
		if(IntYear < 2010)
			IntYear += 543;
		else if(IntYear > 2550)
			IntYear -= 543;
		return (StrDate.substring(3,5)+'-'+StrDate.substring(0,2)+'-'+IntYear);
	}
	
	public static float convertTimeToFloat(String strTime) {
		if (strTime.length() == 5) { // Format HH:MM
			int IntHH = Integer.valueOf(strTime.substring(0, 2)).intValue();
			int IntMM = Integer.valueOf(strTime.substring(3, 5)).intValue();
			return (float) (((IntHH * 60) + IntMM) * 0.000694443333);
		} else {
			return 0f;
		}
	}

	public static String nextDate(String date, int d) {
		for (int i = 0; i < d; i++)
			date = next(date);
		return date;
	}

	static String next(String Date) {
		int d = Integer.valueOf(Date.substring(0, 2)).intValue();
		int m = Integer.valueOf(Date.substring(3, 5)).intValue();
		int y = Integer.valueOf(Date.substring(6, 10)).intValue();

		int dt = 0;

		if (m == 4 || m == 6 || m == 9 || m == 11) {
			dt = 30;
		} else if (m == 2) {
			dt = leapYear(y);
		} else {
			dt = 31;
		}

		if (d < dt) {
			d++;
		} else {
			if (d == dt) {
				d = 1;
				m++;
				if (m > 12) {
					m = 1;
					y++;
				}
			}
		}

		if (d < 10 && m < 10)
			return ("0" + d + "-0" + m + "-" + y);
		else if (d < 10 && m > 9)
			return ("0" + d + "-" + m + "-" + y);
		else if (d > 9 && m < 10)
			return (d + "-0" + m + "-" + y);
		return (d + "-" + m + "-" + y);
	}

	static int leapYear(int theYear) {
		if (theYear < 100) {
			if (theYear > 40) {
				theYear += 1900;
			} else {
				theYear += 2000;
			}
		}

		if (theYear % 4 == 0) {
			if (theYear % 100 != 0) {
				return 29;
			} else if (theYear % 400 == 0) {
				return 29;
			} else {
				return 28;
			}
		} else {
			return 28;
		}
	}
	
	public static String getDateLastMonth(){
		try{
			String StrDate = getDateNow();
			int y = Integer.valueOf(StrDate.substring(6, 10)).intValue();
			int m = Integer.valueOf(StrDate.substring(3, 5)).intValue();
			int d = Integer.valueOf(StrDate.substring(0, 2)).intValue();
			int dt = 0;
			
			if(m == 1){
				m = 12;
				y -= 1;
			}else{
				m -= 1;
			}
			
			if (m == 4 || m == 6 || m == 9 || m == 11) {
				dt = 30;
			} else if (m == 2) {
				dt = leapYear(y);
			} else {
				dt = 31;
			}
			
			if(d > dt){
				d = 1;
				m += 1;
			}
			
			return (d > 9 ? d : ("0"+d)) + "-" + (m > 9 ? m : ("0"+m)) + "-" + y; 
		}catch(Exception e){ 
			return "";
		}
	}
}
